﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace FireZone
{
    internal static class NotifyPropertyChangedExtensions
    {
        public static void NotifyEventAsync<T>(this EventHandler<T> eventHandler, object sender, T e) where T: EventArgs
        {
            if (eventHandler != null)
            {
                if(Application.Current.Dispatcher.CheckAccess())
                {
                    eventHandler(sender, e);
                }
                else
                {
                    Application.Current.Dispatcher.Invoke(new Action<EventHandler<T>, object, T>(NotifyEventAsync), eventHandler, sender, e);
                }
            }
        }
        public static void NotifyEventAsync(this EventHandler eventHandler, object sender, EventArgs e)
        {
            if(eventHandler != null)
            {
                if(Application.Current.Dispatcher.CheckAccess())
                {
                    eventHandler(sender, e);
                }
                else
                {
                    Application.Current.Dispatcher.Invoke(new Action<EventHandler, object, EventArgs>(NotifyEventAsync), eventHandler,  sender, e);
                }
            }
        }
        public static void NotifyPropertyChangedAsync(this PropertyChangedEventHandler e, object sender, params string[] propertyNames)
        {
            if (e != null)
            {
                if(Application.Current.Dispatcher.CheckAccess())
                {
                    foreach (var item in propertyNames)
                    {
                        e(sender, new PropertyChangedEventArgs(item));
                    }
                }
                else
                {                    
                    Application.Current.Dispatcher.Invoke(new Action<PropertyChangedEventHandler, object, string[]>(NotifyPropertyChangedAsync), e, sender, propertyNames);
                }                
            }
        }
    }
}
